package com.example.demo.controller.zabbix;

import com.example.demo.core.domain.AjaxResult;
import com.example.demo.zabbix.Zabbix;
import com.example.demo.zabbix.request.builder.host.HostCreateRequestBuilder;
import com.example.demo.zabbix.request.builder.host.HostDeleteRequestBuilder;
import com.example.demo.zabbix.request.builder.host.HostGetRequestBuilder;
import com.example.demo.zabbix.request.builder.host.HostUpdateRequestBuilder;
import com.example.demo.zabbix.response.BaseResponse;
import net.sf.json.JSONArray;
import org.jsoup.helper.StringUtil;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/host")
public class HostController {

    /**
     * 查询主机列表信息
     */
    @GetMapping("/list")
    public AjaxResult list() {
        String auth = LoginController.cacheData.get("token");
        Zabbix zabbix = LoginController.zabbix;
        AjaxResult ajaxResult = new AjaxResult();
        try {
            BaseResponse<Boolean> hostResponse = zabbix.call(new HostGetRequestBuilder(1L, auth).builder());
            JSONArray array = JSONArray.fromObject(hostResponse.getResult());
            ajaxResult.put("code", 200);
            ajaxResult.put("msg", "查询成功");
            ajaxResult.put("total", array.size());
            ajaxResult.put("data", hostResponse.getResult());
            return ajaxResult;
        } catch (Exception e) {
            return AjaxResult.error("查询失败");
        }
    }

    /**
     * 查询主机信息
     */
    @GetMapping("/getHostInfo")
    public AjaxResult getHostInfo(@RequestParam String hostId) {
        String auth = LoginController.cacheData.get("token");
        Zabbix zabbix = LoginController.zabbix;
        Map<String, Object> maps = new HashMap<>();
        Map<String, List<String>> filterMap = new HashMap<>();
        List<String> hostIds = new ArrayList<>();
        hostIds.add(hostId);
        filterMap.put("hostid", hostIds);
        maps.put("filter", filterMap);
//      检索主机
        try {
            BaseResponse<Boolean> hostResponse = zabbix.call(new HostGetRequestBuilder(1L, auth).setParams(maps).builder());
            JSONArray array = JSONArray.fromObject(hostResponse.getResult());
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.put("code", 200);
            ajaxResult.put("msg", "查询成功");
            ajaxResult.put("total", array.size());
            ajaxResult.put("data", hostResponse.getResult());
            return ajaxResult;
        } catch (Exception e) {
            return AjaxResult.error("查询失败");
        }
    }

    /**
     * @return 新增主机列表信息
     */
    @PostMapping
    public AjaxResult add(String hostName) {
        String auth = LoginController.cacheData.get("token");
        Zabbix zabbix = LoginController.zabbix;
        BaseResponse<Map<String, List<String>>> hostResponse = zabbix.call(new HostCreateRequestBuilder(auth).setHost(hostName).builder());
        if (hostResponse.getResult() == null) {
            return AjaxResult.error("新增主机信息失败");
        } else {
            return AjaxResult.success();
        }
    }

    /**
     * 删除主机列表信息
     */
    @PostMapping("/remove")
    public AjaxResult remove(List<String> ids) {
        String auth = LoginController.cacheData.get("token");
        Zabbix zabbix = LoginController.zabbix;
        BaseResponse<Map<String, List<String>>> hostResponse = zabbix.call(new HostDeleteRequestBuilder(auth).setIds(ids).builder());
        if (hostResponse.getResult() == null) {
            return AjaxResult.error("删除主机信息失败");
        } else {
            return AjaxResult.success();
        }
    }

    /**
     * 更新主机列表信息
     */
    @PutMapping
    public AjaxResult edit(String hostId, String name) {
        String auth = LoginController.cacheData.get("token");
        Zabbix zabbix = LoginController.zabbix;
        Map<String, Object> map = new HashMap<>();
        if (!StringUtil.isBlank(hostId)) {
            map.put("hostid", hostId);
        }
        if (!StringUtil.isBlank(name)) {
            map.put("name", name);
        }
        BaseResponse<Map<String, List<String>>> hostResponse = zabbix.call(new HostUpdateRequestBuilder(auth).setParams(map).builder());
        if (hostResponse.getResult() == null) {
            return AjaxResult.error("更新主机信息失败");
        } else {
            return AjaxResult.success();
        }
    }
}

